package main

import (
	"encoding/json"
	"fmt"
)

type struct_Monster2 struct {
	Name string `json:"name"`
	Age int `json:"age"`
	Birthday string
	Sal float64
	Skill string
}

//结构体反序列化
func testUnmarshalStruct(){
	str := "{\"name\":\"hahaha\",\"age\":20,\"Birthday\":\"2020-01-01\",\"Sal\":34.5,\"Skill\":\"大锤\"}"
	var monster struct_Monster2
	err := json.Unmarshal([]byte(str), &monster)
	if err != nil{
		fmt.Println("反序列化失败", err)
		return
	}
	fmt.Println(monster)
}

//map反序列化
func testUnmarshalMap(){
	str := "{\"address\":\"天上\",\"age\":30,\"name\":\"红孩儿\"}"

	var a map[string]interface{}

	err := json.Unmarshal([]byte(str), &a)
	if err != nil{
		fmt.Println("反序列化失败", err)
		return
	}
	fmt.Println(a)
}

//切片反序列化
func testUnmarshalSlice(){
	str := "[{\"address\":\"天上a\",\"age\":32,\"name\":\"红孩儿er\"}," +
		"{\"address\":[\"天上a\",\"dixia\"],\"age\":32,\"name\":\"红孩儿er\"}]"

	var a []map[string]interface{}

	err := json.Unmarshal([]byte(str), &a)
	if err != nil{
		fmt.Println("反序列化失败", err)
		return
	}
	fmt.Println(a)
}

func main() {
	testUnmarshalStruct()
	testUnmarshalMap()
	testUnmarshalSlice()
}